package com.wildfire.service.impl;

import com.wildfire.dao.OrderDao;
import com.wildfire.dao.ProductDao;
import com.wildfire.dto.OrderDto;
import com.wildfire.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Slf4j
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ProductDao productDao;

    @Async("myTaskExecutor")
    @Override
    public void createOrder(long productId) {
       OrderDto orderDto = new OrderDto();
       orderDto.setProductAmount(1);
       orderDto.setProductId(productId);
       orderDto.setStatus(1);
       orderDao.insertOrder(orderDto);
    }

    @Async("myTaskExecutor")
    @Transactional
    @Override
    public void descStockAndCreateOrder(long productId) {
       productDao.descStock(productId);
       OrderDto orderDto = new OrderDto();
       orderDto.setProductAmount(1);
       orderDto.setProductId(productId);
       orderDto.setStatus(1);
       orderDao.insertOrder(orderDto);
    }


    

}
